<?php

namespace App\Http\Controllers\Notify\SitePay;

use Biz\Constants\Order\OrderConstants;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Biz\Services\Notify\NotifyService;
use Biz\Constants\Pay\PayEaseConstants;
use Biz\Services\Site\SiteOrderService;
use Biz\Constants\Site\SiteOrderConstants;
use Lumen\Payment\PayEase\Facades\PayEase;
use Biz\Services\Notify\NotifyPayEaseService;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * PayEaseController.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class PayEaseController extends BaseController
{
    /**
     * @var mixed
     */
    protected $orderService;

    /**
     * @var mixed
     */
    protected $notifyService;

    /**
     * @var mixed
     */
    protected $notifyPayEaseService;

    /**
     * PayController constructor.
     *
     * @param SiteOrderService     $orderService
     * @param NotifyService        $notifyService
     * @param NotifyPayEaseService $notifyPayEaseService
     */
    public function __construct(SiteOrderService $orderService, NotifyService $notifyService, NotifyPayEaseService $notifyPayEaseService)
    {
        $this->orderService         = $orderService;
        $this->notifyService        = $notifyService;
        $this->notifyPayEaseService = $notifyPayEaseService;
    }

    /**
     * 处理同步支付结果.
     *
     * @param Request $request
     */
    public function handleSync(Request $request)
    {
        $options = $request->all();

        PayEase::handleSync($options, function ($result, $status, $data) {
            /**
             * $result Bool
             * $status String: <hash failed | pay failed | pending | success>
             * $data Array.
             */
            $order_no = $data['v_oid'];

            if (false === $result && 'hash failed' === $status) {
                Log::error(sprintf('订单[%s]签名验证失败', $order_no));

                return;
            }

            $order = $this->orderService->findByOrderNo($order_no);
            if (is_null($order)) {
                Log::error(sprintf('订单[%s]未找到', $order_no));

                return;
            }

            // Check Status
            if (OrderConstants::STATUS_PAY_ING !== $order->status) {
                Log::error(sprintf('订单[%s]状态错误，当前状态为%s', $order_no, $order->status));

                return;
            }

            // 记录
            $data['method'] = PayEaseConstants::METHOD_SYNC;
            $this->notifyPayEaseService->create($data);

            // 更新订单状态
            if (true === $result && 'success' === $status) {
                $this->orderService->success($order);
                Log::info(sprintf('订单[%s]支付成功', $order_no));

                return;
            } elseif (false === $result && 'pay failed' === $status) {
                $this->orderService->failed($order);
                Log::info(sprintf('订单[%s]支付失败', $order_no));

                return;
            }

            //dd($data);
            //Log::info('notify-result-' . json_encode($result));
            //Log::info('notify-status-' . json_encode($status));
            //Log::info('notify-data-' . json_encode($data));
        });
    }
}
